package com.xinwei.springboot.service.impl;

import com.xinwei.springboot.domain.Student;
import com.xinwei.springboot.repository.StudentRepository;
import com.xinwei.springboot.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;
    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @Override
    public Page<Student> findByPage(Integer page, Integer pageSize) {
        Pageable pageable= PageRequest.of(page,pageSize);
        return studentRepository.findAll(pageable);
    }

    @Override
    public void save(Student student) {
        studentRepository.save(student);

    }

    @Override
    public void deleteStudentById(Integer id) {
        studentRepository.deleteById(id);

    }

    @Override
    public void update(Student student) {
        studentRepository.save(student);

    }

    @Override
    public Student findStudentById(Integer id) {
        Optional<Student> optional=studentRepository.findById(id);
        if (optional.isPresent()){
            return optional.get();
        }else{
            log.warn("指定的学生信息[ID:{}]不存在", id);
            return null;
        }
    }
}
